<?php
include_once('config/config.php');

$LoginAdmin = checkAuth();

if($_POST)
{
    $action = $_POST['action'] ?? '';

    if($action == 'delete')
    {
        $id = $_POST['personid'] ?? 0;

        $person = find("SELECT * FROM `pre_person` WHERE `id` = '$id'");

        if(!$person)
        {
            echo json_encode(['code' => 0,'msg' => '该员工不存在','data' => null]);
            exit;
        }

        $result = del('person',"`id` = '$id'");

        if($result['code'] === 1)
        {
            @is_file(ltrim($person['avatar'],'/')) && @unlink(ltrim($person['avatar'],'/'));

            echo json_encode(['code' => 1,'msg' => $result['msg'],'data' => null]);
            exit;
        }else{
            echo json_encode(['code' => 0,'msg' => $result['msg'],'data' => null]);
            exit;
        }
    }
}

// 获取当前页 如果当当前页参数不存在时 默认第一页
$page = $_GET['page'] ?? 1;

// 每页显示多少条数据
$limit = 2;

// 显示页码
$size = 5;

// 获取初始值
$start = ($page - 1) * $limit;

// 获取当前数据表的数据总数
$personCount = find("SELECT COUNT(*) AS count FROM `pre_person` AS `p` LEFT JOIN `pre_department` AS `d` ON `p`.`depid` = `d`.`id` LEFT JOIN `pre_job` AS `j` ON `p`.`jobid` = `j`.`id`");

$PersonData = all("SELECT `p`.*,`d`.`name` AS dname,`j`.`name` AS jname FROM `pre_person` AS `p` LEFT JOIN `pre_department` AS `d` ON `p`.`depid` = `d`.`id` LEFT JOIN `pre_job` AS `j` ON `p`.`jobid` = `j`.`id` ORDER BY `p`.`createtime` DESC LIMIT $start,$limit");

$showPage = showPage($page,$personCount['count'],$limit,$size);

?>

<!DOCTYPE html>
<html lang="en">

<head>
    <!-- 引入样式 -->
    <?php include_once('common/meta.php'); ?>
</head>

<body>

    <!-- 引入头部 -->
    <?php include_once('common/header.php'); ?>

    <!-- 引入菜单栏 -->
    <?php include_once('common/menu.php'); ?>

    <div class="content">
        <div class="header">
            <h1 class="page-title">员工列表</h1>
        </div>
        <ul class="breadcrumb">
            <li><a href="index.php">Home</a> <span class="divider">/</span></li>
            <li class="active">Index</li>
        </ul>

        <div class="container-fluid">
            <div class="row-fluid">
                <div class="btn-toolbar">
                    <button class="btn btn-primary" onClick="location='personAdd.php'"><i class="icon-plus"></i>添加员工</button>
                </div>
                <div class="well">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>员工名称</th>
                                <th>头像</th>
                                <th>性别</th>
                                <th>手机号</th>
                                <th>邮箱</th>
                                <th>部门</th>
                                <th>职位</th>
                                <th>入职时间</th>
                                <th style="width: 26px;"></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach($PersonData as $item) {?>
                                <tr>
                                    <td><?php echo $item['id'];?></td>
                                    <td><?php echo $item['name'];?></td>
                                    <td>
                                        <a href="<?php echo @is_file(ltrim($item['avatar'],'/')) ? $item['avatar'] : '/assets/images/avatar.png';?>" target="_blank">
                                            <img src="<?php echo @is_file(ltrim($item['avatar'],'/')) ? $item['avatar'] : '/assets/images/avatar.png';?>" width="35" alt="">
                                        </a>
                                    </td>
                                    <td>
                                        <?php
                                            switch($item['sex'])
                                            {
                                                case 0:
                                                    echo '保密';
                                                    break;

                                                case 1:
                                                    echo '男';
                                                    break;

                                                case 2:
                                                    echo '女';
                                                    break;
                                            }
                                        ?>
                                    </td>

                                    <td><?php echo $item['mobile']; ?></td>
                                    <td><?php echo $item['email']; ?></td>
                                    <td><?php echo $item['dname']; ?></td>
                                    <td><?php echo $item['jname']; ?></td>
                                    <td><?php echo date('Y-m-d',$item['createtime']); ?></td>
                                    <td>
                                        <a href="personEdit.php?id=<?php echo $item['id']; ?>"><i class="icon-pencil"></i></a>
                                        <a onclick="del(<?php echo $item['id'];?>)" href="#myModal" role="button" data-toggle="modal"><i class="icon-remove"></i></a>
                                    </td>
                                </tr>
                            <?php }?>
                        </tbody>
                    </table>
                </div>
                <!-- <div class="pagination">
                    <ul>
                        <li><a href="#">Prev</a></li>
                        <li><a href="#">1</a></li>
                        <li><a href="#">2</a></li>
                        <li><a href="#">3</a></li>
                        <li><a href="#">4</a></li>
                        <li><a href="#">Next</a></li>
                    </ul>
                </div> -->

                <?php echo $showPage; ?>

                <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h3 id="myModalLabel">删除数据提醒</h3>
                    </div>
                    <div class="modal-body">
                        <p class="error-text"><i class="icon-warning-sign modal-icon"></i>是否确认删除该员工?</p>
                    </div>
                    <div class="modal-footer">
                        <button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
                        <button id="confirm" class="btn btn-danger" data-dismiss="modal">确认</button>
                    </div>
                </div>

                <!-- 引入底部 -->
                <?php include_once('common/footer.php'); ?>
            </div>
        </div>
    </div>

</body>

</html>
<!-- 引入js -->
<?php include_once('common/script.php'); ?>

<script>
    // 全局员工id
    let personid = 0;

    function del(id)
    {
        personid = id;
    }

    $('#confirm').click(function(){
        $.ajax({
            url:'personList.php',
            type:'post',
            data:{
                action:'delete',
                personid,
            },
            dataType:'json',
            success:function(res)
            {
                if(res.code === 1)
                {
                    alert(res.msg);

                    location.reload();

                    return;
                }else{
                    alert('删除失败');
                    return;
                }
            }
        });
    });
</script>